#!/bin/bash
SYNC_DIR=$(dirname $(realpath "$0"))
echo "sync dir: $SYNC_DIR"
repo_array=(
#repo name		29 path					github path		gitee		211 path				branch
LocalPlayer		linux/external/LocalPlayer		LocalPlayer		LocalPlayer	linux/external/LocalPlayer		master
QLauncher		linux/app/QLauncher			QLauncher		QLauncher	linux/app/QLauncher			develop
SDL2			linux/external/SDL2                     none                    none		linux/external/SDL2                     master
adb			linux/external/adb			none			none		linux/external/adb			master
adk			rk/rk1108/framework/adk			none			none		rk/rk1108/framework/adk			master
at_server		rk/rk1108/apps/at_server		none			none		rk/rk1108/apps/at_server		master
avb			linux/external/avb			avb			avb		linux/external/avb			master
alsa-config		linux/external/alsa-config		alsa-config		alsa-config	linux/external/alsa-config		master
alsa-lib		linux/external/alsa-lib			none			none		linux/external/alsa-lib			master
alsa-plugins		linux/external/alsa-plugins		none			none		linux/external/alsa-plugins		master
alsa-utils		linux/external/alsa-utils		none			none		linux/external/alsa-utils		master
broadcom_bsa		linux/external/broadcom_bsa		broadcom_bsa			none		linux/external/broadcom_bsa		master
audioservice		linux/external/audioservice		audioservice		audioservice	linux/external/audioservice		master
bin                     linux/security/bin                      bin                     bin		linux/security/bin                      master
bluez-alsa		linux/external/bluez-alsa		bluez-alsa		bluez-alsa	linux/external/bluez-alsa		master
bsdiff			rk/rk1108/external/bsdiff		none			none		rk/rk1108/external/bsdiff		master
build			rk/rk1108/build				none			none		rk/rk1108/build				master
buildroot		rk/rk1108/buildroot			buildroot		buildroot	rk/rk1108/buildroot			rockchip/2018.02-rc3
buildroot-2016		rk/rk1108/buildroot-2016		none			none		rk/rk1108/buildroot-2016		master
bzip2			rk/rk1108/external/bzip2		none			none		rk/rk1108/external/bzip2		master
camera_engine_rkisp	linux/external/camera_engine_rkisp	camera_engine_rkisp	camera_engine_rkisp linux/external/camera_engine_rkisp	master
cjson			rk/rk1108/external/cjson		none			none		rk/rk1108/external/cjson		master
common			rk/rk1108/common			none			none		rk/rk1108/common			master
conf                    linux/build                             build                   build		linux/build			    yocto-next
config			rk/rk1108/config			none			none		rk/rk1108/config			master
cvr			rk/rk1108/apps/cvr			none			none		rk/rk1108/apps/cvr			master
database		rk/rk1108/framework/database		none			none		rk/rk1108/framework/database		master
demo			linux/app/demo				demo			none		linux/app/demo				master
deviceio_release	linux/external/deviceio_release		deviceio_release	deviceio_release linux/external/deviceio_release	master
distro			linux/distro				distro			distro		linux/distro				master
#dl			linux/buildroot/dl			none			none		linux/buildroot/dl			master
dnsmasq			rk/rk1108/external/dnsmasq		none			none		rk/rk1108/external/dnsmasq		master
docs			linux/docs				docs			docs		linux/docs				master
docs_1108		rk/rk1108/docs				none			none		rk/rk1108/docs				master
device_manager		rk/rk1108/apps/device_manager		none			none		rk/rk1108/apps/device_manager		master
face			rk/rk1108/apps/face			none			none		rk/rk1108/apps/face			master
ffmpeg			linux/external/ffmpeg			ffmpeg			ffmpeg		linux/external/ffmpeg	   rockchip/release/4.0
ffmpeg_1108		linux/external/ffmpeg			ffmpeg			ffmpeg		linux/external/ffmpeg	   rockchip/release/4.1
ffmpeg_debian		linux/external/ffmpeg			ffmpeg			ffmpeg		linux/external/ffmpeg	    rockchip/debian/4.1
freetype		rk/rk1108/external/freetype		none			none		rk/rk1108/external/freetype		master
gstreamer-rockchip	linux/gstreamer-rockchip		none			none		linux/gstreamer-rockchip		master
hal			rk/rk1108/framework/hal			none			none		rk/rk1108/framework/hal			master
hello			rk/rk1108/apps/hello			none			none		rk/rk1108/apps/hello			master
hello_rv		rk/rk1108/apps/hello_rv			none			none		rk/rk1108/apps/hello_rv			master
io                      rk/rk1108/external/io                   none                    none		rk/rk1108/external/io                   master
internal_doc            linux/internal_doc                      none                    none		linux/internal_doc                      master
jhead			rk/rk1108/external/jhead		none			none		rk/rk1108/external/jhead		master
kernel			rk/kernel				kernel			none		rk/kernel			   develop-4.4
kernel-4.19		rk/kernel				kernel			none		rk/kernel			   develop-4.19
kernel_1108		rk/rk1108/kernel			none			none		rk/rk1108/kernel		      fastboot
#kernel-stable-px30	rk/kernel-stable			none			none		rk/kernel-stable	stable-4.4-px30-linux
#kernel-stable-px3se	rk/kernel-stable			none			none		rk/kernel-stable	stable-4.4-px3se-linux
kernel-stable-rk3288	rk/kernel-stable			none			none		rk/kernel-stable	stable-4.4-rk3288-linux
kernel-stable-rk3308	rk/kernel-stable			none			none		rk/kernel-stable	stable-4.4-rk3308-linux
#kernel-stable-rk3326	rk/kernel-stable			none			none		rk/kernel-stable	stable-4.4-rk3326-linux
#kernel-stable-rk3328	rk/kernel-stable			none			none		rk/kernel-stable	stable-4.4-rk3328-linux
kernel-stable-rk3399	rk/kernel-stable			kernel			none		rk/kernel-stable	stable-4.4-rk3399-linux
kernel-stable-rk3399pro	rk/kernel-stable			kernel			none		rk/kernel-stable     stable-4.4-rk3399pro-linux
loader			rk/rk1108/loader			none			none		rk/rk1108/loader			loader
lock			rk/rk1108/apps/lock			none			none		rk/rk1108/apps/lock			master
libcamerahal		rk/rk1108/external/libcamerahal		none			none		rk/rk1108/external/libcamerahal		master
libcutils		linux/external/libcutils		none			none		linux/external/libcutils		master
libcurl			rk/rk1108/external/libcurl		none			none		rk/rk1108/external/libcurl		master
libdrm			android/platform/external/libdrm	libdrm-rockchip		libdrm-rockchip	linux/libdrm-rockchip	  rk33/mid/9.0/develop
libion			rk/rk1108/external/libion		none			none		rk/rk1108/external/libion		master
libjpeg			rk/rk1108/external/libjpeg		none			none		rk/rk1108/external/libjpeg		master
liblog			linux/external/liblog			none			none		linux/external/liblog			master
libmali			linux/libmali				libmali			libmali		linux/libmali				master
libminigui		rk/rk1108/external/libminigui		none			none		rk/rk1108/external/libminigui		master
libnl			rk/rk1108/external/libnl		none			none		rk/rk1108/external/libnl		master
libogg			rk/rk1108/external/libogg		none			none		rk/rk1108/external/libogg		master
libpng			rk/rk1108/external/libpng		none			none		rk/rk1108/external/libpng		master
librkfb			rk/rk1108/external/librkfb		none			none		rk/rk1108/external/librkfb		master
librkrga		rk/rk1108/external/librkrga		none			none		rk/rk1108/external/librkrga		master
librkuvc		rk/rk1108/framework/librkuvc		none			none		rk/rk1108/framework/librkuvc		master
libts			rk/rk1108/external/libts		none			none		rk/rk1108/external/libts		master
libv4l-rkmpp		linux/libv4l-rkmpp			libv4l-rkmpp		libv4l-rkmpp	linux/libv4l-rkmpp			master
libvorbis		rk/rk1108/external/libvorbis		none			none		rk/rk1108/external/libvorbis		master
libvpu			rk/rk1108/external/libvpu		none			none		rk/rk1108/external/libvpu		master
linux-rga		linux/linux-rga				linux-rga		linux-rga	linux/linux-rga				master
live555			rk/rk1108/external/live555		none			none		rk/rk1108/external/live555		master
messenger		rk/rk1108/framework/messenger		none			none		rk/rk1108/framework/messenger		master
meta-rockchip		linux/yocto/meta-rockchip		meta-rockchip		meta-rockchip	linux/yocto/meta-rockchip	    yocto-next
minigui			linux/external/minigui			minigui			minigui		linux/external/minigui			master
model_loader		rk/rk1108/framework/model_loader	none			none		rk/rk1108/framework/model_loader	master
mpp			rk/mpp					mpp			mpp		rk/mpp					develop
mpv			linux/external/mpv			mpv			mpv		linux/external/mpv			master
multicamera		linux/app/multicamera			multicamera		multicamera	linux/app/multicamera			master
multivideoplayer	linux/app/multivideoplayer		multivideoplayer	multivideoplayer linux/app/multivideoplayer		master
nanomsg			rk/rk1108/external/nanomsg		none			none		rk/rk1108/external/nanomsg		master
openssl			rk/rk1108/external/openssl		none			none		rk/rk1108/external/openssl		master
pcba_test		rk/rk1108/apps/pcba_test		none			none		rk/rk1108/apps/pcba_test		master
peephole		rk/rk1108/apps/peephole			none			none		rk/rk1108/apps/peephole			master
prebuild-images		rk/rk1108/prebuild-images		none			none		rk/rk1108/prebuild-images		master
prebuilds		rk/rk1108/prebuilds			none			none		rk/rk1108/prebuilds			master
preisp			rk/rk1108/framework/preisp		none			none		rk/rk1108/framework/preisp		master
process_units		rk/rk1108/framework/process_units	none			none		rk/rk1108/framework/process_units	master
powermanager		linux/external/powermanager		none			none		linux/external/powermanager		master
qcamera			linux/app/qcamera			qcamera			qcamera		linux/app/qcamera			master
qfm			linux/app/qfm				qfm			qfm		linux/app/qfm				master
qplayer			linux/app/qplayer			qplayer			qplayer		linux/app/qplayer			master
recovery		linux/recovery				recovery		recovery	linux/recovery				develop
rk-rootfs-build		linux/rk-rootfs-build			rk-rootfs-build		none		linux/rk-rootfs-build			master
rk_pcba_test		linux/external/rk_pcba_test		rk_pcba_test		rk_pcba_test	linux/external/rk_pcba_test		master
rkcamera		rk/rk1108/framework/rkcamera		none			none		rk/rk1108/framework/rkcamera		master
rkface			rk/rk1108/framework/rkface		none			none		rk/rk1108/framework/rkface		master
rknr			rk/rk1108/framework/rknr		none			none		rk/rk1108/framework/rknr		master
rkffplay		linux/app/rkffplay			none			none		linux/app/rkffplay			master
repo			tools/repo				repo			repo		none					master
rk_tee_user		android/rk/platform/system/rk_tee_user	rk_tee_user		rk_tee_user android/rk/platform/system/rk_tee_user develop-next
rkbin			rk/rkbin				rkbin			rkbin		rk/rkbin				master
rkmedia			linux/rkmedia				rkmedia			rkmedia		linux/rkmedia				master
#rknn-toolkit		rk/rknn-toolkit				rknn-toolkit		none		rk/rknn-toolkit				master
rknn_demo		linux/external/rknn_demo		rknn_demo		rknn_demo	linux/external/rknn_demo		master
rknpu			linux/external/rknpu			rknpu			rknpu		linux/external/rknpu			master
rknpu-fw		linux/external/rknpu-fw		    	rknpu-fw		rknpu-fw	linux/external/rknpu-fw			master
RKNPUTOOLs		android/RKNPUTool		    	RKNPUTools		RKNPUTools	android/RKNPUTool	  rk33/mid/8.1/develop
rkssd			linux/external/rkssd			rkssd			rkssd		linux/external/rkssd			master
rkscript		linux/external/rkscript			rkscript		rkscript	linux/external/rkscript			master
rktoolkit		linux/external/rktoolkit		rktoolkit		rktoolkit	linux/external/rktoolkit		master
rkupdate		linux/external/rkupdate			rkupdate		rkupdate	linux/external/rkupdate			master
rkwifibt		linux/external/rkwifibt			rkwifibt		rkwifibt	linux/external/rkwifibt			master
rockchip		linux/device/rockchip			device-custom		device		linux/device/rockchip			master
settings 		linux/app/settings			settings		settings	linux/app/settings			develop
setting_rk1108		rk/rk1108/framework/settings		none			none		rk/rk1108/framework/settings		master
softapDemo		linux/external/softapDemo		softapDemo		softapDemo	linux/external/softapDemo		master
softapdemo_1108		rk/rk1108/external/softapdemo		none			none		rk/rk1108/external/softapdemo		master
softapServer		linux/external/softapServer		softapServer		softapServer	linux/external/softapServer		master
softapserver_1108	rk/rk1108/external/softapserver		none			none		rk/rk1108/external/softapserver		master
sqlite			rk/rk1108/external/sqlite		none			none		rk/rk1108/external/sqlite		master
syslog-ng		rk/rk1108/external/syslog-ng		none			none		rk/rk1108/external/syslog-ng		master
system_manager		rk/rk1108/apps/system_manager		none			none		rk/rk1108/apps/system_manager		master
tensorflow		linux/external/tensorflow		tensorflow		tensorflow	linux/external/tensorflow		master
tools			linux/tools				tools			tools		linux/tools				master
tools_1108		rk/rk1108/tools				none			none		rk/rk1108/tools				master
tools_1108_device	rk/rk1108/external/tools		none			none		rk/rk1108/external/tools		master
u-boot			android/rk/u-boot			u-boot			u-boot		android/rk/u-boot		      next-dev
update_engine		linux/external/update_engine		update_engine		update_engine	linux/external/update_engine		master
updater			rk/rk1108/apps/updater			none			none		rk/rk1108/apps/updater			master
uvc_app			rk/rk1108/apps/uvc_app			none			none		rk/rk1108/apps/uvc_app			master
vendor_storage		rk/rk1108/framework/vendor_storage	none			none		rk/rk1108/framework/vendor_storage	master
wpa_supplicant          rk/rk1108/external/wpa_supplicant       none                    none		rk/rk1108/external/wpa_supplicant       master
wifiAutoSetup           linux/external/wifiAutoSetup            wifiAutoSetup           none		linux/external/wifiAutoSetup            master
zlib			rk/rk1108/external/zlib			none			none		rk/rk1108/external/zlib			master
)

function sync_repo(){
	len=${#repo_array[@]}
	echo "repo array len is $len"
	for((i=0;i<len;i+=6))
	do
		repo_name=${repo_array[i]}
		path_29=${repo_array[i+1]}
		path_github=${repo_array[i+2]}
		path_gitee=${repo_array[i+3]}
		path_211=${repo_array[i+4]}
		branch=${repo_array[i+5]}

		#echo "the name $repo_name, $path_29,$path_github,$path_gitee,$path_211,$branch..."
		if [ ! -d $repo_name ]; then
			git clone ssh://10.10.10.29:29418/$path_29 -b $branch $repo_name
		fi

		cd $SYNC_DIR/$repo_name
		echo "start syncing $repo_name"
		git pull -f origin $branch:$branch

		if [ $path_github != "none" ]; then
			remote_github="`git remote -v | grep github`"
			if [ -z "$remote_github" ]; then
				git remote add github https://rkchrome:rk83991906@github.com/rockchip-linux/$path_github
			fi
			git fetch github
			git push -f github $branch:$branch
			echo "sync $repo_name to github done"
		fi
                if [ $path_gitee != "none" ]; then
                        remote_gitee="`git remote -v | grep gitee`"
                        if [ -z "$remote_gitee" ]; then
                                git remote add gitee https://rockchip:rk83991906@gitee.com/rockchip/$path_gitee
                        fi
                        git fetch gitee
                        git push -f gitee $branch:$branch
                        echo "sync $repo_name to gitee done"
                fi
		if [ $path_211 != "none" ]; then
			remote_211="`git remote -v | grep server211`"
			if [ -z "$remote_211" ]; then
				git remote add server211 ssh://git@10.10.10.211/linux/$path_211
			fi
			git fetch server211
			git push -f server211 $branch:$branch
			echo "sync $repo_name to server 211 done"
		fi
		cd $SYNC_DIR
	done
}


while true
do
	git pull
	echo -n "start syncing all repo at: "
	date
	sync_repo
	echo -n "finish syncing all repo at: "
	date
	sleep 1h
done
